//
//  DLDatabaseListCell.m
//  AllPass
//
//  Created by apple on 2021/11/3.
//

#import "DLDatabaseListCell.h"
@interface DLDatabaseListCell()
@property(nonatomic,retain)UILabel * nameLab;
@end
@implementation DLDatabaseListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = UIColorWhite;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createCusUI];
    }
    return self;
}
-(void)delBtnClick{
    QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:NSLocalizedString(@"Warm Tips", nil) message:NSLocalizedString(@"Whether to perform the delete operation", nil) preferredStyle:QMUIAlertControllerStyleAlert];
    [alertController addAction:[QMUIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:QMUIAlertActionStyleCancel handler:nil]];
    [alertController addAction:[QMUIAlertAction actionWithTitle:NSLocalizedString(@"确定", nil) style:QMUIAlertActionStyleDestructive handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        NSString *documentsDirectory = [Mytools documentsDirectory];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:self.model.name];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:path error:nil];
        if (self.cellDoneBlock) {
            self.cellDoneBlock();
        }
    }]];
    [alertController showWithAnimated:YES];
}
-(void)editBtnClick{
    QMUIDialogTextFieldViewController *dialogViewController = [[QMUIDialogTextFieldViewController alloc] init];
    dialogViewController.title = NSLocalizedString(@"Rename", nil);
    [dialogViewController addTextFieldWithTitle:@"" configurationHandler:^(QMUILabel *titleLabel, QMUITextField *textField, CALayer *separatorLayer) {
        textField.placeholder = NSLocalizedString(@"Please input something", nil);
    }];
    [dialogViewController addCancelButtonWithText:@"取消" block:nil];
    [dialogViewController addSubmitButtonWithText:@"确定" block:^(QMUIDialogTextFieldViewController *aDialogViewController) {
        UITextField * tf1 = aDialogViewController.textFields.firstObject;
        [aDialogViewController hide];
        //qmui保证内容不会为空
        [self renameDatabase:tf1.text];
    }];
    [dialogViewController show];
}
-(void)renameDatabase:(NSString*)name{
    NSString *newFilename = [name stringByAppendingPathExtension:[self.model.name pathExtension]];
    NSString *documentsDirectory = [Mytools documentsDirectory];
    NSString *oldPath = [documentsDirectory stringByAppendingPathComponent:self.model.name];
    NSString *newPath = [documentsDirectory stringByAppendingPathComponent:newFilename];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:newPath]) {
        XHQHUDErrorText(NSLocalizedString(@"A file already exists with this name", nil));
        return;
    }
    // Move input file into documents directory
    [fileManager moveItemAtPath:oldPath toPath:newPath error:nil];
    if (self.cellDoneBlock) {
        self.cellDoneBlock();
    }
}

- (void)setModel:(DLDatabaseListCellModel *)model{
    _model = model;
    self.nameLab.text = model.name;
    CGFloat maxWid = SCREEN_WIDTH - 10  - 35 * 2;
    CGFloat hei = [model.name sizeWithFont:UIFontMake(16) maxSize:CGSizeMake(maxWid, MAXFLOAT)].height;
    self.nameLab.frame = Frame(10, 20, maxWid, hei);
    model.cellHeight = ceilf(hei + 40);
}
-(void)createCusUI{
    self.nameLab = [UILabel labelWithTitle:@"" color:TextColor fontSize:16 alignment:NSTextAlignmentLeft];
    [self.contentView addSubview:self.nameLab];
    
    UIButton * delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [delBtn setImage:Image(@"删除分组") forState:UIControlStateNormal];
    [delBtn addTarget:self action:@selector(delBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:delBtn];
    delBtn.sd_layout.rightSpaceToView(self.contentView, 0).centerYEqualToView(self.contentView).heightIs(35).widthEqualToHeight();
    
    UIButton * editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [editBtn setImage:Image(@"编辑分组名称") forState:UIControlStateNormal];
    [editBtn addTarget:self action:@selector(editBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:editBtn];
    editBtn.sd_layout.rightSpaceToView(delBtn, 0).centerYEqualToView(self.contentView).heightIs(35).widthEqualToHeight();
}

@end
